﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class ProductDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get all products in DB
        /// </summary>
        /// <returns>List of product</returns>
        public List<Product> GetListProducts()
        {
            try
            {
                var resultQuery = from product in _entities.Products
                                  where product.IsDeleted == false
                                  select product;
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get a category by CategoryId
        /// </summary>
        /// <param name="categoryId">CategoryId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public Product GetProductById(int productId)
        {
            try
            {
                var resultQuery = (from product in _entities.Products
                                   where product.ID == productId
                                         && product.IsDeleted == false
                                   select product).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Create a Product
        /// </summary>
        /// <param name="category">Product object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool CreateProduct(Product product)
        {
            try
            {
                _entities.Products.Add(product);
                var resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Update a Product
        /// </summary>
        /// <param name="areaId">Product object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool UpdateProduct(Product product)
        {
            try
            {
                _entities.Entry(product).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Delete a Product
        /// </summary>
        /// <param name="categoryId">productId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool DeleteProduct(int productId)
        {
            try
            {
                Product productEnt = new Product();
                productEnt = (from product in _entities.Products
                               where product.ID == productId
                               select product).FirstOrDefault();
                // Set flag delete for Product
                productEnt.IsDeleted = true;
                _entities.Entry(productEnt).State = EntityState.Modified;
                var resultDelete = _entities.SaveChanges();
                if (resultDelete > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Check is existed a product when update a product
        /// </summary>
        /// <param name="categoryId">Id</param>
        /// <param name="categoryName">Name</param>
        /// <returns>True: Existed -- False: Not existed</returns>
        public bool IsExistedProductCreate(string productName, int categoryId)
        {
            try
            {
                var selectResult = (from product in _entities.Products
                                    where product.Name == productName
                                          && product.CategoryId == categoryId
                                    select product).FirstOrDefault();
                if (selectResult != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Check is existed a product when update a product
        /// </summary>
        /// <param name="categoryId">Id</param>
        /// <param name="categoryName">Name</param>
        /// <returns>True: Existed -- False: Not existed</returns>
        public bool IsExistedProductUpdate(int productId, string productName, int categoryId)
        {
            try
            {
                var selectResult = (from product in _entities.Products
                                    where product.ID != productId
                                          && product.Name == productName
                                          && product.CategoryId == categoryId
                                    select product).FirstOrDefault();
                if (selectResult != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}